package com.getword.web.controller;

import com.getword.domain.Article;
import com.getword.domain.User;
import com.getword.security.SUser;
import com.getword.service.ArticleService;
import com.getword.web.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/api/article")
public class ApiArticleController {
    @Autowired
    private ArticleService articleService;

    // 分页查询, articleTitle,user.userName,
    @GetMapping
    public Object query(@PageableDefault(page = 1, size = 10) Pageable page, Article article, HttpServletRequest request) {
//        SUser user = (SUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        article.setUser(new Use);
        return articleService.getArticles(page, article);
    }

    // 分页查询, articleTitle,user.userName,
    // 返回layui需要的格式 {"code":0,"msg":"","count":1000,"data":[]
    @GetMapping("/query")
    public Object query2(int page, int size, Article article, HttpServletRequest request) {
//        SUser user = (SUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Pageable pageable = PageRequest.of(page-1, size);
        Page<Article> articles = articleService.getArticles(pageable,article);
        Map<String, Object> result = new HashMap<>();
        if(articles!=null){
            result.put("code", 0);
            result.put("msg", "");
            result.put("count", articles.getTotalElements());
            result.put("data", articles.getContent());
        }else{
            result.put("code", 1);
            result.put("msg", "服务器错误");
            result.put("count", 0);
        }

        return result;
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public Object getInfo(@PathVariable String id){
        Result result = articleService.getArticleById(id);
        return result;
    }

    // 判断文章是否点赞了e
    @GetMapping("/zanState")
    public Result zanState(String articleId){
        return articleService.getZanState(articleId);
    }


    // 点赞
    @PostMapping("/zan")
    public Object zan(String articleId) {
        Map<String, Object> data = new HashMap<>();
        return articleService.zan(articleId);
    }
    // 获取点赞个数
    @GetMapping("/zanCount")
    public Result zanCount(String articleId){
        return articleService.zanCount(articleId);
    }

    // 判断文章是否收藏了e
    @GetMapping("/collectState")
    public Result collectState(String articleId){
        return articleService.getCollectState(articleId);
    }

    @PostMapping("/collect")
    public Result collect(String articleId){
        return articleService.collect(articleId);
    }

    // 保存文章

    /**
     * @param article  title, content,'outlineDetail.outlineId', 'outlineDetail.num'
     * @return
     */
    @PostMapping
    public Object saveArticle(Article article){
        SUser user = (SUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = user.getUserId();
//        article.setOutlineDetail(new OutlineDetail(outlineDetailId));
        article.setUser(new User(userId));
        article.setReleaseTime(new Date());
        return articleService.saveArticle(article);
    }

    // 更新
    @PutMapping("/{id}")
    public Object updateArticle(@PathVariable String id, String title, String html){
        return articleService.updateArticle(html, title, id);
    }

    @DeleteMapping("/{id}")
    public Result deleteArticle(@PathVariable String id){
        return articleService.deleteArticle(id);
    }
}
